package com.ruoyi.project;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.Md5Utils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.channel.channelAccount.domain.ChannelAccount;
import com.ruoyi.project.pay.WBRedPackageUtil;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class WeiBo {


    public static void main(String args[]) {
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                try {
//                    String cookiePayer = "SUB=_2A25w5XnXDeRhGeFN71cS-C_FzjqIHXVQeSGfrDV8PUJbitAKLXn8kWtNQApj95EsdGroSonZWCguYF-PH9F4uCXA";
////        WeiBo.getPayUrl(20.00,1,cookiePayer );
//                    JSONObject jsonObject = WBRedPackageUtil.checkOrder("104445179919428200",cookiePayer);
//                    JSONObject data = jsonObject.getJSONObject("data");
//                    JSONArray array = data.getJSONArray("biz");
//                    JSONObject result = array.getJSONObject(0);
//                    System.out.println(result.getString("order_id")+";"+result.getString("status"));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        },0,30 * 1000);
        // 微博1  SUB=_2A25w4ajhDeRhGeFN71cX-S3Fzz-IHXVQfNKprDV8PUJbitANLVDEkWtNQApHaEbmyhVKb0c7EdtTckq3Fy321FPv&UID=7345693943
        String cookie3 = "SUB=_2A25w4ajhDeRhGeFN71cX-S3Fzz-IHXVQfNKprDV8PUJbitANLVDEkWtNQApHaEbmyhVKb0c7EdtTckq3Fy321FPv";
        String user3="7345693943";
        // 微博2
        String cookie2 ="SUB=_2A25w5XZVDeRhGeFN71cX9C3OyzqIHXVQeSQdrDV8PUJbitAfLWLFkWtNQAplb19ZarI-PZHUYIpqSsDugng8CkP4";
        String user2 ="7345643206";

        // 微博2
        String cookie1 ="SUB=_2A25w5XnXDeRhGeFN71cS-C_FzjqIHXVQeSGfrDV8PUJbitAKLXn8kWtNQApj95EsdGroSonZWCguYF-PH9F4uCXA";
        String user1 ="7345381956";
        // 微博4
        String cookie4 ="SUB=_2A25w4nSbDeRhGeFN71oT8y3Lzz-IHXVQfCbTrDV8PUJbitAKLXn3kWtNQA6a-pqxdl6hntJu_c_lfHdZ5F0CQ0hL";
        String user4 ="7348233743";
//         WBRedPackageUtil.checkOrder("104445401302503832",cookiePayer);
//        WeiBo.getPayUrl(20.00,1,cookiePayer );
        JSONObject jsonObject = WBRedPackageUtil.checkOrder("104446078322405905",cookie4);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray array = data.getJSONArray("biz");
        JSONObject result = array.getJSONObject(0);
        if(result.getString("status").equals("2")){
            // 2 代表已经付款
            String orderId = result.getString("order_id");
            JSONObject receiveObj = WBRedPackageUtil.sendRedpackage(orderId,cookie4,user2);
            // {"code":"100000","msg":"\u64cd\u4f5c\u6210\u529f","data":{"url":"sinaweibo:\/\/browser\/close"}}
            System.out.println("receiveObj:"+receiveObj.toJSONString());
            if(receiveObj.getString("code").equals("100000")){
                // 通知成功 开始拆包
                String res = WBRedPackageUtil.pickMoney(orderId, cookie2);
                System.out.println("res:"+res);
                if(res.contains("红包金额已存入您的钱包")){
                    // 已经收到红包了
                   System.out.println("红包金额已存入您的钱包");
                }
            }
        }
    }

    /**
     * 获取支付的html
     *
     * @param money 总金额
     * @param count 红包个数
     */
    public static String getPayUrl(double money, int count, String cookiePayer) {
        double singalAmount = money / count; // 单个红包金额
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("X-Requested-With", "XMLHttpRequest");
        headers.add("Referer", "https://mall.e.weibo.com/h5/redenvelope/create?page=2");
        headers.add("User-Agent", "Mozilla/5.0 (Linux; Android 5.1.1; DUK-AL20 Build/LMY48Z) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Safari/537.36 Weibo (HUAWEI-DUK-AL20__weibo__9.7.2.1__android__android5.1.1)");
        headers.add("Cookie", cookiePayer);
        String data ="bag_type=1&puicode=0&msgtype=0&msgid=0&singalAmount="+singalAmount+"&amount=" + money + "&count=" + count + "&blessing=快来抢，手快有，手慢无。恭喜发财！&_t=0";
        HttpEntity entity = new HttpEntity(data, headers);
        String order = restTemplate.exchange("https://mall.e.weibo.com/aj/redenvelope/create", HttpMethod.POST, entity, String.class).getBody();
        System.out.println("setToJHNextOrder result:" + order);

        JSONObject objectOrder = JSONObject.parseObject(order);
        String url = objectOrder.getJSONObject("data").getString("url");
        String userAgent = "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Mobile Safari/537.36";
        HashMap<String ,String > map = new HashMap<>();
        map.put("User-Agent",userAgent);
        map.put("Cookie",cookiePayer);
        String result= HttpUtils.sendGet(url,map,"");
        System.out.println("setToJHNextOrder result:" + result);
        result = result.replace("<!DOCTYPE HTML><html>\t<head>\t<meta charset=\"utf-8\">\t<title>微博红包</title>\t</head>\t<body>","").replace("<script>\t\tdocument.getElementById('alipayForm').submit();\t</script>\t</body></html>","");
        System.out.println("setToJHNextOrder result:" + result);
        return result;
    }
}
